package com.mall.db.api;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.mall.db.mapper.SmsCouponProductCategoryRelationMapper;
import com.mall.pojo.SmsCouponProductCategoryRelation;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@DubboService
public class SmsCouponProductCategoryRelationApiImpl implements SmsCouponProductCategoryRelationApi {

    @Autowired
    private SmsCouponProductCategoryRelationMapper couponProductCategoryRelationMapper;

    /**
     * 添加商品分类与优惠券关系
     *
     * @param productCategoryRelationList
     */
    @Override
    public void addIntoProductCategoryRelation(List<SmsCouponProductCategoryRelation> productCategoryRelationList) {
        for (SmsCouponProductCategoryRelation relation : productCategoryRelationList) {
            couponProductCategoryRelationMapper.insert(relation);
        }
    }

    /**
     * 根据优惠券id删除商品分类与优惠券关系
     *
     * @param id
     */
    @Override
    public void deleteByCouponId(Integer id) {
        LambdaQueryWrapper<SmsCouponProductCategoryRelation> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SmsCouponProductCategoryRelation::getCouponId, id);
        couponProductCategoryRelationMapper.delete(wrapper);
    }

    /**
     * 根据优惠券id查询商品分类与优惠券关系
     *
     * @param id
     * @return
     */
    @Override
    public List<SmsCouponProductCategoryRelation> findByCouponId(Integer id) {
        LambdaQueryWrapper<SmsCouponProductCategoryRelation> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SmsCouponProductCategoryRelation::getCouponId, Convert.toLong(id));
        return couponProductCategoryRelationMapper.selectList(wrapper);
    }

    /**
     * 修改到品分类与优惠券关系
     *
     * @param productCategoryRelationList
     */
    @Override
    public void updateIntoProductCategoryRelation(List<SmsCouponProductCategoryRelation> productCategoryRelationList) {
        for (SmsCouponProductCategoryRelation relation : productCategoryRelationList) {
            couponProductCategoryRelationMapper.updateById(relation);
        }
    }
}
